package com.it.reggie;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.it.reggie.entity.*;
import com.it.reggie.service.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.core.RedisTemplate;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

@SpringBootTest
@SuppressWarnings("all")
public class test {
    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private RedisTemplate redisTemplate;


    @Test
    void method1() {

        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();

        //根据菜品分类，计算相同菜品价格，查询菜品价格和菜品名称
        queryWrapper.select("sum(amount) as value", "name");
        queryWrapper.groupBy("setmeal_id!=null", "setmeal_id");

        //存储查询结果
        List<Map<String, Object>> listMaps = orderDetailService.listMaps(queryWrapper);

        System.out.println("===========================");
        System.out.println(listMaps);

    }


    @Test
    void method2() {

        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();

        //根据菜品分类，计算相同菜品价格，查询菜品价格和菜品名称
        queryWrapper.select("sum(amount) as value", "name");
        queryWrapper.groupBy("dish_id");

        //存储查询结果
        List<Map<String, Object>> listMaps = orderDetailService.listMaps(queryWrapper);

        System.out.println("===========================");
        System.out.println(listMaps);

        for (Map<String, Object> listMap : listMaps) {

            //遍历获取每一个菜品名称
            String name = (String) listMap.get("name");

            LambdaQueryWrapper<Dish> dishqw = new LambdaQueryWrapper<>();

            dishqw.eq(Dish::getName, name);
            //根据菜品名称查询菜品数据
            Dish one = dishService.getOne(dishqw);
            if (one != null) {
                //获取菜系id
                Long categoryId = one.getCategoryId();

                //根据id获取菜系名称
                Category categoryById = categoryService.getById(categoryId);


                //给每一个菜品的名称都换成菜系名称
                listMap.put("name", categoryById.getName());


            }

        }


        for (int i = 0; i < listMaps.size(); i++) {

            for (int y = 0; y < listMaps.size(); y++) {
                //遍历判断结果
                if (listMaps.get(i).get("name").equals(listMaps.get(y).get("name")) && i != y) {

                    BigDecimal value = (BigDecimal) listMaps.get(i).get("value");
                    BigDecimal value1 = (BigDecimal) listMaps.get(y).get("value");
                    BigDecimal valueSum = value.add(value1);
                    System.out.println("0000000000000000000000000000");
                    System.out.println(valueSum);

                    listMaps.get(y).put("value", valueSum);

                    listMaps.remove(listMaps.get(i));

                    break;

                }
            }
            System.out.println(listMaps);
        }

        System.out.println("//////////////////////////////////");
        System.out.println(listMaps);
    }

    @Test
    void
    method() {

        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        System.out.println(now.toLocalDate());


    }

    @Test
    void methods() {

        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();

        queryWrapper.select(" sum(amount)", "checkout_time");
        queryWrapper.groupBy("checkout_time");

        List<Map<String, Object>> listMaps = ordersService.listMaps(queryWrapper);

        List list1 = new ArrayList();
        List list2 = new ArrayList();

        for (Map<String, Object> listMap : listMaps) {

            Set<Map.Entry<String, Object>> entries = listMap.entrySet();

            for (Map.Entry<String, Object> entry : entries) {

                if (entry.getKey().equals("checkout_time")) {

                    list1.add(entry.getValue().toString());
                } else {
                    list2.add(entry.getValue());
                }

            }

        }
        System.out.println(list1);
        System.out.println(list2);

        Map map = new HashMap();

        map.put("monthList", list1);
        map.put("dataList", list2);

//        listMaps.stream().map(new Function<Map<String, Object>, Object>() {
//            @Override
//            public Object apply(Map<String, Object> stringObjectMap) {
//
//
//
//                LocalDateTime checkOutTime = (LocalDateTime) stringObjectMap.get("checkout_time");
//
//                return checkOutTime.toLocalDate();
//
//            }
//        });

        System.out.println(listMaps);


    }

    //10 13 16 19
    @Test
    void method0() {

        int count = 0;
        int sum = 0;
        for (int i = 10; ; i = i + 3) {
            sum = sum + i;
            count++;
            if (sum >= 1000) {
                System.out.println(count);
                break;
            }
        }
    }

    @Test
    void redisTest(){

//       redisTemplate.opsForValue().set("aa", "aa");

        System.out.println(redisTemplate.opsForValue().get("aa"));

        redisTemplate.opsForValue().set("11", "112", 30, TimeUnit.SECONDS);

    }
}

